package cn.sinata.wanglin.village.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.MakecomplaintsAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Makecomplaints;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 * description：小区-->小区动态-->吐槽
 * Created by kaihongwu on 2016/12/06 上午 9:45
 */
public class MakecomplaintsFragment extends RecyclerViewFragment {
    private List<Makecomplaints> mMakecomplaints=new ArrayList<Makecomplaints>();
    private MakecomplaintsAdapter mMakecomplaintsAdapter;
    private int pageNo = 0;
    public static MakecomplaintsFragment newInstance(){
        MakecomplaintsFragment mFragment = new MakecomplaintsFragment();
        return mFragment;
    }
    @Override
    protected RecyclerView.Adapter setAdapter() {
        if(mMakecomplaintsAdapter==null){
            HorizontalDividerItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(context)
                    .size(DensityUtil.dip2px(context,10))
                    .color(ContextCompat.getColor(context, R.color.dividing_line_color_wanglin))
                    .build();
            setItemDecoration(itemDecoration);
            mMakecomplaintsAdapter=new MakecomplaintsAdapter(mMakecomplaints,getContext(),MakecomplaintsFragment.this);
        }
        return mMakecomplaintsAdapter;
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }

    @Override
    protected void onFirstVisibleToUser() {
           setEmptyViewText("邻居们都在上面等你");
        setEmptyViewClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
                pullDownRefresh();
            }
        });
               getData();

    }

    private void getData() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getHousingDynamic(userid,MyApplication.houseid,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Makecomplaints>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Makecomplaints> data) {
                        if (pageNo == 0) {
                            mMakecomplaints.clear();
                        }
                        if (data.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mMakecomplaints.addAll(data);
                        mMakecomplaintsAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
    public void reRefresh(){
        showDialog();
        Utils.systemErr("---吐槽刷新-");
        pullDownRefresh();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Utils.systemErr("=resultCod-->" + resultCode);
        Utils.systemErr("requestCode-->" + requestCode);
        if(resultCode==-1){
            switch (requestCode)
            {
                case 12:
                    showDialog("刷新中...");
                    pullDownRefresh();
                    break;
            }

        }
    }
}
